package com;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.widget.Toast;

public class Nav {
    public static final String KExtraReferrer = "referrer";
    private final Context mContext;
    private final Intent mIntent;
    private int mRequestCode = -1;
    private boolean mAllowLeaving;
    private boolean mDisallowLoopback;
    private boolean mSkipPreprocess;
    private boolean mSkipHooker;
    private boolean mSkipPriorHooker;
    private boolean mDisableTransition;
    private static int[] mTransition;
    private List<Intent> mTaskStack;
    private static final List<Nav.NavPreprocessor> mPreprocessor = new CopyOnWriteArrayList();
    private static final List<Nav.NavPreprocessor> mStickPreprocessor = new ArrayList();
    private static final SparseArray<Nav.NavHooker> mPriorHookers = new SparseArray();
    private static Nav.NavExceptionHandler mExceptionHandler;
    private static final Nav.NavResolver DEFAULT_RESOLVER = new Nav.DefaultResovler();
    private static volatile Nav.NavResolver mNavResolver;
    private static final String TAG = "Nav";

    public static Nav from(Context context) {
        return new Nav(context);
    }

    public Nav withCategory(String category) {
        this.mIntent.addCategory(category);
        return this;
    }

    public Nav withExtras(Bundle extras) {
        if (extras == null) {
            return this;
        } else {
            this.mIntent.putExtras(extras);
            return this;
        }
    }

    public Nav withFlags(int flags) {
        this.mIntent.addFlags(flags);
        return this;
    }

    public Nav forResult(int request_code) {
        if (!(this.mContext instanceof Activity)) {
            throw new IllegalStateException("Only valid from Activity, but from " + this.mContext);
        } else {
            this.mRequestCode = request_code;
            return this;
        }
    }

    public Nav allowEscape() {
        this.mAllowLeaving = true;
        return this;
    }

    public Nav disallowLoopback() {
        this.mDisallowLoopback = true;
        return this;
    }

    public Nav disableTransition() {
        this.mDisableTransition = true;
        return this;
    }

    public Nav skipPreprocess() {
        this.mSkipPreprocess = true;
        return this;
    }

    public Nav skipHooker() {
        this.mSkipHooker = true;
        return this;
    }

    public Nav skipPriorHooker() {
        this.mSkipPriorHooker = true;
        return this;
    }

    public boolean toUri(NavUri uri) {
        return this.toUri(uri.build());
    }

    public boolean toUri(String uri) {
        return TextUtils.isEmpty(uri) ? false : this.toUri(Uri.parse(uri));
    }

    public boolean toUri(Uri uri) {
        Log.d("Nav", uri.toString());
        Nav.NavExceptionHandler exception_handler = mExceptionHandler;
        Intent intent = this.to(uri);
        if (intent == null) {
            if (exception_handler != null) {
                exception_handler.onException(this.mIntent,
                    new Nav.NavigationCanceledException("Intent resolve was null"));
            }

            return false;
        } else if (intent instanceof Nav.NavHookIntent) {
            return true;
        } else if (this.mContext == null) {
            if (exception_handler != null) {
                exception_handler.onException(this.mIntent,
                    new Nav.NavigationCanceledException("Context shouldn\'t null"));
            }

            Log.e("Nav", "Nav context was null");
            return false;
        } else {
            while (true) {
                try {
                    ComponentName e = null;
                    ResolveInfo url;
                    List lenth;
                    ResolveInfo rinfo;
                    if (this.mAllowLeaving) {
                        url = mNavResolver.resolveActivity(this.mContext.getPackageManager(), intent, 65536);
                        if (url != null) {
                            e = new ComponentName(url.activityInfo.packageName, url.activityInfo.name);
                        } else {
                            lenth = mNavResolver.queryIntentActivities(this.mContext.getPackageManager(), intent,
                                65536);
                            rinfo = lenth != null && lenth.size() >= 1 ? (ResolveInfo)lenth.get(0) : null;
                            if (rinfo == null) {
                                throw new ActivityNotFoundException("No Activity found to handle " + intent);
                            }

                            e = new ComponentName(rinfo.activityInfo.packageName, rinfo.activityInfo.name);
                        }
                    } else if (!RuntimeGlobals.isMultiPackageMode(this.mContext)) {
                        intent.setPackage(this.mContext.getPackageName());
                        url = mNavResolver.resolveActivity(this.mContext.getPackageManager(), intent, 65536);
                        if (url == null) {
                            lenth = mNavResolver.queryIntentActivities(this.mContext.getPackageManager(), intent,
                                65536);
                            rinfo = this.optimum(lenth);
                            if (rinfo == null) {
                                throw new ActivityNotFoundException("No Activity found to handle " + intent);
                            }

                            intent.setClassName(rinfo.activityInfo.packageName, rinfo.activityInfo.name);
                        } else {
                            intent.setClassName(url.activityInfo.packageName, url.activityInfo.name);
                        }

                        e = intent.getComponent();
                    } else {
                        List url1 = mNavResolver.queryIntentActivities(this.mContext.getPackageManager(), intent,
                            65536);
                        ResolveInfo lenth1 = this.optimum(url1);
                        if (lenth1 == null) {
                            throw new ActivityNotFoundException("No Activity found to handle " + intent);
                        }

                        intent.setClassName(lenth1.activityInfo.packageName, lenth1.activityInfo.name);
                        e = intent.getComponent();
                    }

                    if (this.mDisallowLoopback && this.mContext instanceof Activity && e != null && e.equals(
                        ((Activity)this.mContext).getComponentName())) {
                        Log.w("Nav", "Loopback disallowed: " + uri);
                        return false;
                    }

                    if (this.mTaskStack != null && VERSION.SDK_INT >= 11) {
                        this.mTaskStack.add(this.mIntent);
                        this.startActivities((Intent[])this.mTaskStack.toArray(new Intent[this.mTaskStack.size()]));
                    } else if (this.mRequestCode >= 0) {
                        ((Activity)this.mContext).startActivityForResult(intent, this.mRequestCode);
                    } else {
                        if (!(this.mContext instanceof Activity)) {
                            intent.addFlags(268435456);
                        }

                        this.mContext.startActivity(intent);
                    }

                    if (!this.mDisableTransition && mTransition != null && this.mContext instanceof Activity) {
                        ((Activity)this.mContext).overridePendingTransition(mTransition[0], mTransition[1]);
                    }

                    if (this.isDebug()) {
                        String url2 = intent.getData().toString();
                        int lenth2 = url2.length();
                        if (lenth2 > 5120) {
                            Toast.makeText(this.mContext,
                                "Your url : " + url2 + " is too large which may cause Exception, plz check it!", Toast.LENGTH_SHORT)
                                .show();
                        }
                    }

                    return true;
                } catch (ActivityNotFoundException var8) {
                    if (this.isDebug()) {
                        Toast.makeText(this.mContext, uri.toString() + " CANN\'T FOUND RESOLVED ACTIVITY", Toast.LENGTH_SHORT).show();
                    }

                    if (exception_handler == null || !exception_handler.onException(intent, var8)) {
                        return false;
                    }

                    exception_handler = null;
                }
            }
        }
    }

    @TargetApi(11)
    private void startActivities(Intent[] intents) {
        this.mContext.startActivities(intents);
    }

    public PendingIntent toPendingUri(Uri uri, int requestCode, int flags) {
        Intent intent = this.specify(this.to(uri, false));
        if (intent == null) {
            return null;
        } else if (this.mTaskStack != null && VERSION.SDK_INT >= 11) {
            this.mTaskStack.add(this.mIntent);
            return getActivities(this.mContext, requestCode,
                (Intent[])this.mTaskStack.toArray(new Intent[this.mTaskStack.size()]), flags);
        } else {
            intent.addFlags(268435456);
            return PendingIntent.getActivity(this.mContext, requestCode, intent, flags);
        }
    }

    @TargetApi(11)
    private static PendingIntent getActivities(Context context, int requestCode, Intent[] intents, int flags) {
        return getActivities(context, requestCode, intents, flags);
    }

    @TargetApi(11)
    public Nav stack(Uri uri) {
        if (this.mRequestCode >= 0) {
            throw new IllegalStateException("Cannot stack URI for result");
        } else {
            Intent intent = new Intent(this.to(uri, false));
            if (this.mTaskStack == null) {
                this.mTaskStack = new ArrayList(1);
                intent.addFlags(268435456);
                if (VERSION.SDK_INT >= 11) {
                    intent.addFlags('쀀');
                }
            }

            this.mTaskStack.add(intent);
            Nav nav = new Nav(this.mContext);
            nav.mTaskStack = this.mTaskStack;
            return nav;
        }
    }

    private Intent to(Uri uri) {
        return this.to(uri, !this.mSkipPreprocess);
    }

    private Intent to(Uri uri, boolean preprocess) {
        this.mIntent.setData(uri);
        Nav.NavHooker hooker = (Nav.NavHooker)mPriorHookers.get(4);
        if (!this.mSkipHooker && hooker != null && !hooker.hook(this.mContext, this.mIntent)) {
            return new Nav.NavHookIntent();
        } else {
            if (!this.mSkipPriorHooker) {
                for (int i$ = 0; i$ < mPriorHookers.size(); ++i$) {
                    int preprocessor = mPriorHookers.keyAt(i$);
                    if (preprocessor != 4) {
                        hooker = (Nav.NavHooker)mPriorHookers.get(preprocessor);
                        if (hooker != null && !hooker.hook(this.mContext, this.mIntent)) {
                            return new Nav.NavHookIntent();
                        }
                    }
                }
            }

            if (!this.mIntent.hasExtra("referrer")) {
                if (this.mContext instanceof Activity) {
                    Intent var7 = ((Activity)this.mContext).getIntent();
                    if (var7 != null) {
                        Uri var9 = var7.getData();
                        if (var9 != null) {
                            this.mIntent.putExtra("referrer", var9.toString());
                        } else {
                            ComponentName comp;
                            if ((comp = var7.getComponent()) != null) {
                                this.mIntent.putExtra("referrer", (new Intent()).setComponent(comp).toUri(0));
                            } else {
                                this.mIntent.putExtra("referrer", var7.toUri(0));
                            }
                        }
                    }
                } else {
                    this.mIntent.putExtra("referrer", this.mContext.getPackageName());
                }
            }

            Iterator var8;
            Nav.NavPreprocessor var10;
            if (!mStickPreprocessor.isEmpty()) {
                var8 = mStickPreprocessor.iterator();

                while (var8.hasNext()) {
                    var10 = (Nav.NavPreprocessor)var8.next();
                    if (!var10.beforeNavTo(this.mIntent)) {
                        return null;
                    }
                }
            }

            if (preprocess && !mPreprocessor.isEmpty()) {
                var8 = mPreprocessor.iterator();

                while (var8.hasNext()) {
                    var10 = (Nav.NavPreprocessor)var8.next();
                    if (!var10.beforeNavTo(this.mIntent)) {
                        return null;
                    }
                }
            }

            return this.mIntent;
        }
    }

    private Intent specify(Intent intent) {
        if (!this.mAllowLeaving) {
            List list = mNavResolver.queryIntentActivities(this.mContext.getPackageManager(), intent, 65536);
            ResolveInfo info = this.optimum(list);
            if (info == null) {
                return intent;
            }

            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
        }

        return intent;
    }

    private ResolveInfo optimum(List<ResolveInfo> list) {
        if (list == null) {
            return null;
        } else if (list.size() == 1) {
            return (ResolveInfo)list.get(0);
        } else {
            ArrayList resolveInfo = new ArrayList();
            Iterator ret = list.iterator();

            while (ret.hasNext()) {
                ResolveInfo info = (ResolveInfo)ret.next();
                if (!TextUtils.isEmpty(info.activityInfo.packageName)) {
                    if (info.activityInfo.packageName.endsWith(this.mContext.getPackageName())) {
                        resolveInfo.add(new Nav.SortedResolveInfo(info, info.priority, 1));
                    } else {
                        String p1 = info.activityInfo.packageName;
                        String p2 = this.mContext.getPackageName();
                        String[] l1 = p1.split("\\.");
                        String[] l2 = p2.split("\\.");
                        if (l1.length >= 2 && l2.length >= 2 && l1[0].equals(l2[0]) && l1[1].equals(l2[1])) {
                            resolveInfo.add(new Nav.SortedResolveInfo(info, info.priority, 0));
                        }
                    }
                }
            }

            if (resolveInfo.size() > 0) {
                if (resolveInfo.size() > 1) {
                    Collections.sort(resolveInfo);
                }

                ResolveInfo ret1 = ((Nav.SortedResolveInfo)resolveInfo.get(0)).info;
                resolveInfo.clear();
                return ret1;
            } else {
                return null;
            }
        }
    }

    public static void registerStickPreprocessor(Nav.NavPreprocessor preprocessor) {
        mStickPreprocessor.add(preprocessor);
    }

    public static void unregisterStickPreprocessor(Nav.NavPreprocessor preprocessor) {
        mStickPreprocessor.remove(preprocessor);
    }

    public static void registerPreprocessor(Nav.NavPreprocessor preprocessor) {
        mPreprocessor.add(preprocessor);
    }

    public static void unregisterPreprocessor(Nav.NavPreprocessor preprocessor) {
        mPreprocessor.remove(preprocessor);
    }

    public static void registerHooker(Nav.NavHooker hooker) {
        mPriorHookers.put(4, hooker);
    }

    public static void registerPriorHooker(Nav.NavHooker hooker, int priority) {
        if (priority <= 3 && priority >= 1) {
            mPriorHookers.put(priority, hooker);
        } else {
            throw new RuntimeException(
                "NavHooker\'s priority less than NAVHOOKER_HIGH_PRIORITY, larger than NAVHOOKER_LOW_PRIORITY");
        }
    }

    public static void setExceptionHandler(Nav.NavExceptionHandler handler) {
        mExceptionHandler = handler;
    }

    public static void setNavResolver(Nav.NavResolver resolver) {
        mNavResolver = resolver;
    }

    public static void setTransition(int enterAnim, int exitAnim) {
        mTransition = new int[2];
        mTransition[0] = enterAnim;
        mTransition[1] = exitAnim;
    }

    private Nav(Context context) {
        this.mContext = context;
        this.mIntent = new Intent("android.intent.action.VIEW");
    }

    private boolean isDebug() {
        ApplicationInfo app_info = this.mContext.getApplicationInfo();
        return (app_info.flags & 2) != 0;
    }

    static {
        mNavResolver = DEFAULT_RESOLVER;
    }

    private static class NavHookIntent extends Intent {
        private NavHookIntent() {
        }
    }

    private static final class DefaultResovler implements Nav.NavResolver {
        private DefaultResovler() {
        }

        @Override
        public List<ResolveInfo> queryIntentActivities(PackageManager pm, Intent intent, int flags) {
            return pm.queryIntentActivities(intent, flags);
        }

        @Override
        public ResolveInfo resolveActivity(PackageManager pm, Intent intent, int flags) {
            return pm.resolveActivity(intent, flags);
        }
    }

    private final class SortedResolveInfo implements Comparable<Nav.SortedResolveInfo> {
        private final ResolveInfo info;
        private int weight = 0;
        private int same = 0;

        public SortedResolveInfo(ResolveInfo info, int weight, int same) {
            this.info = info;
            this.weight = weight;
            this.same = same;
        }

        public int compareTo(Nav.SortedResolveInfo other) {
            return this == other ? 0 : (other.weight != this.weight ? other.weight - this.weight
                : (other.same != this.same ? other.same - this.same : (System.identityHashCode(this) < System
                    .identityHashCode(other) ? -1 : 1)));
        }
    }

    public static class NavigationCanceledException extends Exception {
        private static final long serialVersionUID = 5015146091187397488L;

        public NavigationCanceledException(String s) {
            super(s);
        }
    }

    public interface NavExceptionHandler {
        boolean onException(Intent var1, Exception var2);
    }

    public interface NavHooker {
        int NAVHOOKER_HIGH_PRIORITY = 3;
        int NAVHOOKER_NORMAL_PRIORITY = 2;
        int NAVHOOKER_LOW_PRIORITY = 1;
        int NAVHOOKER_STICKMAX_PRIORITY = 4;

        boolean hook(Context var1, Intent var2);
    }

    public interface NavPreprocessor {
        boolean beforeNavTo(Intent var1);
    }

    public interface NavResolver {
        List<ResolveInfo> queryIntentActivities(PackageManager var1, Intent var2, int var3);

        ResolveInfo resolveActivity(PackageManager var1, Intent var2, int var3);
    }

    @SuppressLint({"Registered"})
    static class DemoActivity extends Activity {
        DemoActivity() {
        }

        void openItem(long item_id) {
            Nav.from(this).toUri(NavUri.host("item.taobao.com").path("item.htm").param("id", item_id));
        }

        void buildTaskStack(Uri parent, Uri target) {
            Nav.from(this).stack(parent).toUri(target);
        }

        void openUriWithinWebview(Uri uri) {
            Nav nav = Nav.from(this);
            if (!nav.disallowLoopback().toUri(uri) && nav.skipPreprocess().allowEscape().toUri(uri)) {
                ;
            }

        }
    }
}
